package labox.innovation.gameserver.network.clientpackets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.db.PlayFriends;
import labox.innovation.gameserver.model.FriendInfo;
import labox.innovation.gameserver.model.FuzeWorld;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;

/**
 * @author ljsh
 * @date 2010-3-30 
 * @version 1.0
 * @description 关于删除好友的请求
 */
public final class RequestFriendDel extends L2GameClientPacket {

	private static final String _C__61_REQUESTFRIENDDEL = "[C] 61 RequestFriendDel";

	private static Logger _log = LoggerFactory.getLogger(RequestFriendDel.class.getName());

	private long objId;

	@Override
	protected void readImpl() {
		objId = readLong();
	}

	@Override
	protected void runImpl() {
		SystemMessage sm;
		FuzePcInstance activeChar = getClient().getActiveChar();
		if (activeChar == null)
			return;
		FriendInfo info = activeChar.getFriendInstance().getFriendInfo(objId);
		if (info == null) {
			sm = new SystemMessage(SystemMessageId.C1_NOT_ON_YOUR_FRIENDS_LIST);
			activeChar.sendPacket(sm);
			return;
		}
		String _name = info.getName();
		sm = new SystemMessage(SystemMessageId.S1_HAS_BEEN_DELETED_FROM_YOUR_FRIENDS_LIST);
		sm.addString(_name);
		activeChar.sendPacket(sm);
		_log.info(activeChar.getObjectId() + " delete friend " + objId);
		activeChar.getFriendInstance().removrFriendInfo(objId);
		FuzePcInstance target = FuzeWorld.getInstance().getPlayer(objId);
		if (target == null) {
			//执行sql操作，将activeChar从子表objId记录中删除
			PlayFriends.deleteplayer_Friends(objId, activeChar);
		} else {
			target.getFriendInstance().getHeroesId().remove(activeChar.getObjectId());
		}

	}

	@Override
	public String getType() {
		return _C__61_REQUESTFRIENDDEL;
	}
}
